package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProject;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * @Description NursingPlanImpl
 * @Author hu hanSan
 * @Date 2024-09-08
 */
@Service
public class NursingPlanImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询所有护理计划
     *
     * @return
     */
    @Override
    public List<NursingPlanVo> selectAllPlan() {
        List<NursingPlanVo>list= nursingPlanMapper.selectAllPlan();
        return list;
    }

    /**
     * 添加护理计划
     *
     * @param nursingPlanDto
     */
    @Transactional //开启事务
    @Override
    public void addPlan(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        // 设置默认值
        if (nursingPlan.getStatus() == null) {
            nursingPlan.setStatus(0); // 假设默认值为 0
        }
        nursingPlanMapper.insertPlan(nursingPlan);

        //获取id
        Long id = nursingPlan.getId();
        //获取项目集合
        List<NursingProjectPlan> projectPlans = nursingPlan.getProjectPlans();
        //if (!CollectionUtils.isEmpty(projectPlans)){
           // projectPlans.forEach(nursingProjectPlan ->nursingProjectPlan.setPlanId(id));
           for (NursingProjectPlan projectPlan : projectPlans) {
                projectPlan.setPlanId(id);
           // }

            //添加护理项目计划
             nursingProjectPlanMapper.insertProjectPlan(projectPlans);
        }

    }

    /**
     * 条件分页查询
     *
     * @return
     */
    @Override
    public PageResponse<NursingPlanVo> getByPage(String name,Integer status,Integer pageNum,Integer pageSize) {
        //设置分页参数
        PageHelper.startPage(pageNum,pageSize);
        Page<NursingPlanVo>page= nursingPlanMapper.getByPage(name,status);
        //封装对象并返回
        PageResponse<NursingPlanVo> pageResponse = new PageResponse<>(page);
        //没有赋值，需手动赋值
        pageResponse.setRecords(page.getResult());
        return pageResponse;
    }

    /**
     * 根据id查询护理计划
     *
     * @param id
     * @return
     */
    @Override
    public NursingPlanVo selectById(Long id) {
        NursingPlan nursingPlan= nursingPlanMapper.selectById(id);
        List<NursingProjectPlan>nursingProjectPlans= nursingProjectPlanMapper.selectByPlanId(id);
        NursingPlanVo nursingPlanVo = BeanUtil.toBean(nursingPlan, NursingPlanVo.class);
        nursingPlanVo.setProjectPlans(nursingProjectPlans);
        return nursingPlanVo;
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlanDto
     */
    @Transactional
    @Override
    public void update(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlan.setUpdateTime(LocalDateTime.now());
        nursingPlanMapper.updatePlan(nursingPlan);

        Long id = nursingPlan.getId();
        //删除对应的所有护理项目信息
        nursingProjectPlanMapper.deleteAllProjectPlanByPlanId(id);

        List<NursingProjectPlan> projectPlans = nursingPlan.getProjectPlans();
        if (!CollectionUtils.isEmpty(projectPlans)) {
            projectPlans.forEach(nursingProjectPlan -> nursingProjectPlan.setPlanId(id));
        }
        //新增护理项目
        nursingProjectPlanMapper.insertProjectPlan(projectPlans);
    }


    /**
     * 根据id来删除护理计划
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        //删除护理计划
        nursingPlanMapper.deletePlanById(id);
        //删除与护理计划相关联的项目信息
        nursingProjectPlanMapper.deleteAllProjectPlanByPlanId(id);
    }

    /**
     * 根据id来设置启用和禁用
     *
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Integer id, Integer status) {
        nursingPlanMapper.updateStatus(id,status);
    }
}
